﻿@model MvcCms.Web.ViewModels.AccountModel
@{           
    ViewBag.Title = ViewData["SiteName"] + " - " + ViewData["PageTitle"];    
}
@section HtmlHead{
}
<div class="contentpage-outerdiv">
    <h2>
        @ViewData["PageTitle"]</h2>
    <div>
        <table width="600" cellspacing="0" cellpadding="5" border="0">
            <tr>
                <td>
                    <span style="display: inline-block; width: 400px;">
                        <table border="0" style="vertical-align: middle;">
                            <tbody>
                                <tr>
                                    <td valign="top">
                                    </td>
                                    <td align="center" rowspan="2">
                                        <select class="users_select" id="availableUsers" multiple="multiple" name="availableUsers" size="15">
                                            @if (Model._UsersNotInRole != null)
                                            {
                                                foreach (var user in Model._UsersNotInRole)
                                                {                                                        
                                                <option value="@user.UserID">@user.UserName</option>
                                                }
                                            }
                                        </select>
                                    </td>
                                    <td width="100px" align="center" rowspan="2">
                                        <input type="button" id="toRight" value="&gt;&gt;"><br>
                                        <input type="button" id="toLeft" value="&lt;&lt;">
                                    </td>
                                    <td align="center" rowspan="2">
                                        <select class="users_select" multiple="multiple" id="currentUsers" name="currentUsers" size="15">
                                            @if (Model._UsersInRole != null)
                                            {
                                                foreach (var user in Model._UsersInRole)
                                                {
                                                <option value="@user.UserID">@user.UserName</option>
                                                }
                                            }
                                        </select>
                                    </td>
                                    <td valign="top">
                                        <img src="/content/images/busy.gif" style="display: none;" id="savingUser" />
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </span>
                </td>
            </tr>
        </table>
    </div>
</div>
@Html.Hidden("RoleID", Model._Role.RoleId)
@section ScriptContent{
    <script id="Add/Remove User(s)" type="text/javascript">
        $(document).ready(function () {
            var suppliedSelect = jQuery('#availableUsers');
            jQuery('#toRight').click(function () {
                suppliedSelect.find(':selected').each(function (index, elem) {
                    var selectElem = $(elem);
                    if (selectElem.val()) {
                        AddUser(selectElem.val());
                        selectElem.appendTo('#currentUsers');
                    }
                });
            });
        });
        $(document).ready(function () {
            var suppliedSelect = jQuery('#currentUsers');
            jQuery('#toLeft').click(function () {
                suppliedSelect.find(':selected').each(function (index, elem) {
                    var selectElem = $(elem);
                    if (selectElem.val()) {
                        RemoveUser(selectElem.val());
                        selectElem.appendTo('#availableUsers');
                    }
                });
            });
        });
    </script>
    <script id="Add/RemoveUser" type="text/javascript">
        function RemoveUser(userId) {
            $("#savingUser").show();
            $.post("/accountajax/removeuserfromrole/",
		    { RoleID: $('#RoleID').val(), UserID: userId },
            function (data) {
                if (data.error) {
                    alert(data.error);
                }
                $("#savingUser").hide();
            }, "json");
        }
        function AddUser(userId) {
            $("#savingUser").show();
            $.post("/accountajax/addusertorole/",
		    { RoleID: $('#RoleID').val(), UserID: userId, Confirmed: true, useHTML: true },
            function (data) {
                if (data.error) {
                    alert(data.error);
                }
                $("#savingUser").hide();
            }, "json");
        }    
    </script>
}
